var testCasePassRateData = new Array();
var testCasePassRateList = new Array();
var testOverview = {
		//验收报告与测试总览切换触发事件
		changeDisplay : function(){
			if(textFlag == "testOverview"){
				textFlag = "acceptReport";
				$(".testOverview_title>h4").text("验收报告");
				$(".testOverview_title>a").text("切换测试总览");
				$('#testOverview').hide();
				$('#acceptReport').show();
			}else if(textFlag == "acceptReport"){
				textFlag = "testOverview";
				$(".testOverview_title>h4").text("测试总览");
				$(".testOverview_title>a").text("切换验收报告");
				$('#testOverview').show();
				$('#acceptReport').hide();
			}
			
			
		},
		//获取迭代列表
		getIterationList : function(){
			
			$.ajax({
			    type : "get",
			    url : path+'/testCaseController/listTreeOnTestCase?type=iteration',//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){

		    	},
			    success : function(dataList) {
			    	//加载已有工作项下拉列表
			    	$("#iteration").html("<option value=''>全部</option>");
			    	$(dataList).each(function(index,data){
			    		if(data.parentId != ""){
			    			$("#iteration").append("<option value="+data.id+">"+data.name+"</option>");
			    		}
			    		
					});
			    },
			    complete: function(XMLHttpRequest, textStatus){


		    	}
			   });
			
		},
		//迭代切换触发事件
		iterationChange : function(){
			iterationId = $('#iteration option:selected').val();
			testOverview.getTestCaseRate();
			testOverview.initTestPlanList();
			testOverview.initAssignerOrAuthor();
			//testOverview.initBugCountList();
			
		},
		//获取用例的通过率和完成率
		getTestCaseRate : function(){
			$.ajax({
			    type : "get",
			    url : path+'/testCaseController/getTestCaseRate?iterationId='+iterationId,//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){
			    	myChart_a1.showLoading();
			    	myChart_a2.showLoading();
		    	},
			    success : function(dataMap) {
			    	console.log("dataMap: "+JSON.stringify(dataMap));
			    	//用例的通过率模块
			    	var resultCountMap = dataMap.resultCount;
			    	if(resultCountMap != null){
			    		$('.main_a1_noData').addClass('hidden');
			    		$('#main_a1').removeClass('hidden');
			    		$('.main_a1_dataShow').removeClass('hidden');
			    		var pass_totalCount = 0;
				    	var casePassCount = 0;
//			 	    	main_a1_pass main_a1_failure main_a1_stop main_a1_notApply main_a1_activity
				    	var resultCountData = [];
				    	for(var key in resultCountMap){
				    		resultCountData.push({value:resultCountMap[key], name:key});
				    		if(key == "通过"){
				    			pass_totalCount += resultCountMap[key];
				    			casePassCount = resultCountMap[key];
				    			$('.main_a1_pass').text(resultCountMap[key]);
				    		}else if(key == "失败"){
				    			pass_totalCount += resultCountMap[key];
				    			$('.main_a1_failure').text(resultCountMap[key]);
				    		}else if(key == "阻止"){
				    			pass_totalCount += resultCountMap[key];
				    			$('.main_a1_stop').text(resultCountMap[key]);
				    		}else if(key == "不适用"){
				    			pass_totalCount += resultCountMap[key];
				    			$('.main_a1_notApply').text(resultCountMap[key]);
				    		}else if(key == "活动"){
				    			pass_totalCount += resultCountMap[key];
				    			$('.main_a1_activity').text(resultCountMap[key]);
				    		}

				    	}
				    	option_a1.series[0].data = resultCountData;
				    	myChart_a1.clear();
				    	myChart_a1.setOption(option_a1);
				    	myChart_a1.resize();
						 $(window).resize(myChart_a1.resize);
						 var casePassRate = ((casePassCount/pass_totalCount)*100).toFixed(2) + "%";
						 console.log("casePassRate： "+casePassRate);
						 $('#testCasePassRate').text("用例通过率: "+casePassRate);
			    	}else{
//		 	    		main_a1_noData main_a1 main_a1_dataShow
			    		$('.main_a1_noData').removeClass('hidden');
			    		$('#main_a1').addClass('hidden');
			    		$('.main_a1_dataShow').addClass('hidden');
			    	}


					//用例的完成率模块
				   var stateCountMap = dataMap.stateCount;
				   if(stateCountMap != null){
					   $('.main_a2_noData').addClass('hidden');
			    		$('#main_a2').removeClass('hidden');
			    		$('.main_a2_dataShow').removeClass('hidden');
					   var complete_totalCount = 0;
				    	var caseCompleteCount = 0;
//				    	main_a1_ready main_a1_design main_a1_closed
				    	var stateCountData = [];
				    	for(var key in stateCountMap){
				    		stateCountData.push({value:stateCountMap[key], name:key});
				    		if(key == "就绪"){
				    			complete_totalCount += stateCountMap[key];
				    			$('.main_a1_ready').text(stateCountMap[key]);
				    		}else if(key == "设计"){
				    			complete_totalCount += stateCountMap[key];
				    			$('.main_a1_design').text(stateCountMap[key]);
				    		}else if(key == "已关闭"){
				    			caseCompleteCount = stateCountMap[key];
				    			complete_totalCount += stateCountMap[key];
				    			$('.main_a1_closed').text(stateCountMap[key]);
				    		}

				    	}
				    	option_a2.series[0].data = stateCountData;
				    	myChart_a2.clear();
				    	myChart_a2.setOption(option_a2);
				    	myChart_a2.resize();
						 $(window).resize(myChart_a2.resize);
						 
//		 				 newChart.resize();
//		 				 $(window).resize(myChart.resize);
						 
						 
						 var caseCompleteRate = ((caseCompleteCount/complete_totalCount)*100).toFixed(2) + "%";
						 console.log("caseCompleteRate： "+caseCompleteRate);
						 $('#testCaseCompleteRate').text("用例完成率: "+caseCompleteRate);

				   }else{
					   $('.main_a2_noData').removeClass('hidden');
			    		$('#main_a2').addClass('hidden');
			    		$('.main_a2_dataShow').addClass('hidden');

				   }



			    },
			    complete: function(XMLHttpRequest, textStatus){
			    	myChart_a1.hideLoading();
			    	myChart_a2.hideLoading();

		    	}
			   });
			
			
		},
		overViewA1 : function(param){
			console.log(param.name);
			var paramResult = "";
			switch(param.name){
				case "活动":
					paramResult = "0";
					break;
				case "通过":
					paramResult = "1";
					break;
				case "失败":
					paramResult = "2";
					break;
				case "阻止":
					paramResult = "3";
					break;
				case "不适用":
					paramResult = "4";
					break;
			
			}
			window.location.href=path+"/testPlan?paramResult="+paramResult; 
			
			
		}, 
		overViewA2 : function(param){
			console.log(param.name);
			
			window.location.href=path+"/testCase"; 
			
			
		}, 
		initTestPlanList : function(){
			$.ajax({
			    type : "get",
			    url : path+'/testCaseController/getTestCaseResultByTestPlan?iterationId='+iterationId,//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){
			    	/*myChart_a1.showLoading();
			    	myChart_a2.showLoading();*/
		    	},
			    success : function(data) {
			    	testCasePassRateData = [].concat(data);
			    	testCasePassRateList = [].concat(data);
			    	var testPlanDataList = data;/*[{"id":"id1","name":"测试计划1","pass":2,"failure":4,"stop":6,"notApply":8,"activity":10,"total":30},
						{"id":"id2","name":"测试计划2","pass":1,"failure":3,"stop":5,"notApply":7,"activity":9,"total":25},
						{"id":"id3","name":"测试计划3","pass":2,"failure":4,"stop":6,"notApply":8,"activity":10,"total":30},
						{"id":"id4","name":"测试计划4","pass":1,"failure":3,"stop":5,"notApply":7,"activity":9,"total":25}];*/
					$('.testPlanList').html("");
					var testPlanListHtml = '';
					if (testPlanDataList.length > 0) {
						$(testPlanDataList).each(function(index,testPlanData){
							var barHtml = '';
							if(testPlanData.pass != 0){
							barHtml += '<div style="width: '+(testPlanData.pass/testPlanData.total)*100+'%; background-color: #5183c1;"></div>';
							}
							if(testPlanData.fail != 0){
							barHtml += '<div style="width: '+(testPlanData.fail/testPlanData.total)*100+'%; background-color: #979797;"></div>';
							}
							if(testPlanData.block != 0){
							barHtml += '<div style="width: '+(testPlanData.block/testPlanData.total)*100+'%; background-color: #ffce54;"></div>';
							}
							if(testPlanData.notApply != 0){
							barHtml += '<div style="width: '+(testPlanData.notApply/testPlanData.total)*100+'%; background-color: #3dcca6;"></div>';
							}
							if(testPlanData.active != 0){
							barHtml += '<div style="width: '+(testPlanData.active/testPlanData.total)*100+'%; background-color: #f83;"></div>';
							}
							var style = '';
							if(index == 0){
							
							style = 'style="padding-left:7.5px!important;"';
							}else if(index == testPlanDataList.length-1){
							
							style = 'style="padding-right:7.5px!important;"';
						
						}
						testPlanListHtml +='<div class="col-sm-2" '+style+'>'
												+'<div class="checkbox" style="white-space:nowrap;">'
													+'<input id="'+testPlanData.id+'" class="magic-checkbox" type="checkbox" checked="checked" onclick="testOverview.checkboxCheck(this)">'
													+'<label for="'+testPlanData.id+'">'+testPlanData.name+'</label>'
												+'</div>'
												+'<div  class="overview-bar">'
													+barHtml
												+'</div>'
											+'</div>';
						
						})
						
						if (testPlanDataList.length > 18) {
							$('.testPlanList').css("height","150px");
							$('.testPlanList').css("overflow","auto");
						}
					}
					$('.testPlanList').append(testPlanListHtml);
			    },
			    complete: function(XMLHttpRequest, textStatus){
			    	/*myChart_a1.hideLoading();
			    	myChart_a2.hideLoading();*/
			    	testOverview.initTestPlanTable();
		    	}
			   });
		},
		initTestPlanTable : function(){
			 
			    	var testPlanTableList =testCasePassRateList;/* [{"testPlanId":"id1","testPlanNm":"测试计划1","pass":2,"failure":4,"stop":6,"notApply":8,"activity":10,"total":30},
						{"testPlanId":"id2","testPlanNm":"测试计划2","pass":1,"failure":3,"stop":5,"notApply":7,"activity":9,"total":25}];*/

					$('#testPlanTable>tbody').html("");
					var tbodyHtml = '';
					if (testPlanTableList.length > 0) {
						$(testPlanTableList).each(function(index,planData){
							tbodyHtml += '<tr>'
										+'<td><a href="'+path+'/testPlan?testPlanId='+planData.id+'">'+planData.name+'</a></td>'
										+'<td>'+planData.pass+'</td>'
										+'<td>'+planData.fail+'</td>'
										+'<td>'+planData.block+'</td>'
										+'<td>'+planData.notApply+'</td>'
										+'<td>'+planData.active+'</td>'
										+'<td>'+planData.total+'</td>'
									+'</tr>';
							
						});
						//设置表格div高度 
						$('.div1').css("height","200px");
						$('.div1').css("overflow","auto");
					} else {
						tbodyHtml += '<tr>'
						+'<td></td><td></td><td></td><td>无数据</td><td></td><td></td><td></td>'
						+'</tr>';
					}
					
					$('#testPlanTable>tbody').append(tbodyHtml);
		},
		initAssignerOrAuthor : function(){
			$('.assigner-div').html('');
			$('.author-div').html('');
			$('.assigner-div').append('<div class="panel-title">指派者统计</div>');
			$('.author-div').append('<div class="panel-title">创建者统计</div>');
			
			$.ajax({
			    type : "get",
			    url : path+'/testCaseController/getTestCaseStateByUser?iterationId='+iterationId,//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){
			     
		    	},
			    success : function(data) {
			    	var assignerOrAuthorList = data;/*[{"type":"assigner","userAccount":"user1","userId":"userId1","userName":"name1","pass":2,"failure":4,"stop":6,"notApply":8,"activity":10,"total":30},
						{"type":"assigner","userAccount":"user2","userId":"userId2","userName":"name2","pass":1,"failure":3,"stop":5,"notApply":7,"activity":9,"total":25},
						{"type":"author","userAccount":"user3","userId":"userId3","userName":"name3","pass":2,"failure":4,"stop":6,"notApply":8,"activity":10,"total":30},
						{"type":"author","userAccount":"user4","userId":"userId4","userName":"name4","pass":1,"failure":3,"stop":5,"notApply":7,"activity":9,"total":25}];*/
			    	
			    	var assigner = data.assigner;
			    	var author = data.author;
			    	
					var assignerDiv = "";
					var authorDiv = "";
					
					if (assigner == undefined || assigner.length == 0) {
						assignerDiv += '<div style="text-align: center;">无数据</div>';
					}
					$(assigner).each(function(index,userData){
						var barHtml = '';
						if(userData.ready != 0){
						barHtml += '<div style="width: '+(userData.ready/userData.total)*100+'%; background-color: #5183c1;" title="就绪：'+userData.ready+'"></div>';
						}
						if(userData.design != 0){
						barHtml += '<div style="width: '+(userData.design/userData.total)*100+'%; background-color: #979797;" title="设计中：'+userData.design+'"></div>';
						}
						if(userData.closed != 0){
						barHtml += '<div style="width: '+(userData.closed/userData.total)*100+'%; background-color: #ffce54;" title="已关闭：'+userData.closed+'"></div>';
						}
						//barHtml += '<div style="width:10%;margin-top: -4px;">'+userData.total+'</div>';
						
						
						assignerDiv += '<div class="panel media middle pad-all">'
						+'<div class="media-left">'
						+'<span class="icon-wrap icon-wrap-sm icon-circle bg-success">'
							+'<i class="demo-pli-male icon-2x"></i>'
						+'</span>'
						+'</div>'
						+'<div class="media-body">'
						+'<p class="text-2x mar-no text-semibold">'+userData.name+" ： "+userData.total+'</p>'
						+'<div class="overview-bar">'
							+barHtml  
						+'</div>'
						+'</div>'
						+'</div>';
					});
					
					if (author == undefined || author.length == 0) {
						authorDiv = '<div style="text-align: center;">无数据</div>';
					}
					$(author).each(function(index,userData){
						var barHtml = '';
						if(userData.ready != 0){
						barHtml += '<div style="width: '+(userData.ready/userData.total)*100+'%; background-color: #5183c1;" title="就绪：'+userData.ready+'"></div>';
						}
						if(userData.design != 0){
						barHtml += '<div style="width: '+(userData.design/userData.total)*100+'%; background-color: #979797;" title="设计中：'+userData.design+'"></div>';
						}
						if(userData.closed != 0){
						barHtml += '<div style="width: '+(userData.closed/userData.total)*100+'%; background-color: #ffce54;" title="已关闭：'+userData.closed+'"></div>';
						}
						//barHtml += '<div style="width:10%;margin-top: -4px;">'+userData.total+'</div>';
						
						authorDiv += '<div class="panel media middle pad-all">'
						+'<div class="media-left">'
						+'<span class="icon-wrap icon-wrap-sm icon-circle bg-success">'
							+'<i class="demo-pli-male icon-2x"></i>'
						+'</span>'
						+'</div>'
						+'<div class="media-body">'
						+'<p class="text-2x mar-no text-semibold">'+userData.name+" ： "+userData.total+'</p>'
						+'<div class="overview-bar">'
							+barHtml
						+'</div>'
						+'</div>'
						+'</div>';
					});
					
					
					/*$(assignerOrAuthorList).each(function(index,userData){
						
						var barHtml = '';
						if(userData.pass != 0){
						barHtml += '<div style="width: '+(userData.pass/userData.total)*100+'%; background-color: #5183c1;"></div>';
						}
						if(userData.failure != 0){
						barHtml += '<div style="width: '+(userData.failure/userData.total)*100+'%; background-color: #979797;"></div>';
						}
						if(userData.stop != 0){
						barHtml += '<div style="width: '+(userData.stop/userData.total)*100+'%; background-color: #ffce54;"></div>';
						}
						if(userData.notApply != 0){
						barHtml += '<div style="width: '+(userData.notApply/userData.total)*100+'%; background-color: #3dcca6;"></div>';
						}
						if(userData.activity != 0){
						barHtml += '<div style="width: '+(userData.activity/userData.total)*100+'%; background-color: #f83;"></div>';
						}
						
						
						if(userData.type == "assigner"){
						
						assignerDiv += '<div class="panel media middle pad-all">'
						+'<div class="media-left">'
						+'<span class="icon-wrap icon-wrap-sm icon-circle bg-success">'
							+'<i class="demo-pli-male icon-2x"></i>'
						+'</span>'
						+'</div>'
						+'<div class="media-body">'
						+'<p class="text-2x mar-no text-semibold">'+userData.userName+'</p>'
						+'<div class="overview-bar">'
							+barHtml
						+'</div>'
						+'</div>'
						+'</div>';
						
						}else if(userData.type == "author"){
						authorDiv += '<div class="panel media middle pad-all">'
						+'<div class="media-left">'
						+'<span class="icon-wrap icon-wrap-sm icon-circle bg-success">'
							+'<i class="demo-pli-male icon-2x"></i>'
						+'</span>'
						+'</div>'
						+'<div class="media-body">'
						+'<p class="text-2x mar-no text-semibold">'+userData.userName+'</p>'
						+'<div class="overview-bar">'
							+barHtml
						+'</div>'
						+'</div>'
						+'</div>';
						
						}
					});*/
					$('.assigner-div').append(assignerDiv);
					$('.author-div').append(authorDiv);
			    },
			    complete: function(XMLHttpRequest, textStatus){
			     
		    	}
			});
			
			
			
//			<div class="panel media middle pad-all">
//				<div class="media-left">
//					<span class="icon-wrap icon-wrap-sm icon-circle bg-success" >
//						<i class="demo-pli-male icon-2x"></i>
//					</span>
//				</div>
//				<div class="media-body" >
//					<p class="text-2x mar-no text-semibold">用户A</p>
//					<div class="overview-bar">
//						<div style="width: 15%; background-color: #979797;"></div>
//						<div style="width: 40%; background-color: #5183c1;"></div>
//						<div style="width: 45%; background-color: #3dcca6;"></div>
//					</div>
//				</div>
//		</div>
		},
		
		checkboxCheck: function(data){
			var id = data.id;
			if(data.checked){//由没选中变成选中，添加进数组
				$(testCasePassRateData).each(function(index,planData){
					if (planData.id == id) {
						testCasePassRateList.splice(index,0,planData);
					}
				});
			} else {//有选中变成没选中，移除数组
				$(testCasePassRateList).each(function(index,planData){
					if (planData.id == id) {
						testCasePassRateList.remove(index);
					}
				});
			}
			//重新加载列表
			testOverview.initTestPlanTable();
		},
		
		
		initBugCountList : function(){
			//bugStatus
			$('.bugStatus').append('<div class="panel-title">缺陷</div>');
			$.ajax({
			    type : "get",
			    url : path+'/testCaseController/countJiraBug?iterationId='+iterationId,//找到controller里相应的方法
			    beforeSend: function(XMLHttpRequest){
			     
		    	},
			    success : function(data) { 
			    	if (data.status == null || data.status == undefined) {
			    		var emptyHtml = '';
			    		emptyHtml += '<tr>'
							+'<td></td><td></td><td></td><td>无数据</td><td></td><td></td><td></td>'
							+'</tr>';
			    		$('#bugCountTable').append(emptyHtml);
			    	} else {
			    		var bugStatus = data.status;
				    	var bugCount = data.result;
				    	var statusHtml = '';
				    	/*
						<div class="overview-list-legend-item">
				            <div style="background-color: rgb(255, 136, 51);"></div>
				            <span>关键</span>
				        </div>*/
				    	//随机生成颜色
				    	var color = [];
				    	for (var i = 0; i < bugStatus.length; i ++) {
				    		color.push(testOverview.getRandomColor());
				    	}
				    	
				    	var thread = '<thead><tr><th>是否关联CTS</th>';
				    	
				    	$(bugStatus).each(function(index,status){
				    		statusHtml += '<div class="overview-list-legend-item">'
				    			+'<div style="background-color: '+color[index]+'"></div>'
				    			+' <span>'+status.name+'</span>'
				    			+'</div>';
				    		
				    		thread += '<th>'+status.name+'</th>';
				    	});
				    	$('.bugStatus').append(statusHtml);
				    	
				    	
				    	//列表
				    	$('#bugCountTable').html("");
				    	thread += '<th>合计</th></tr></thread>'
				    	$('#bugCountTable').append(thread);
				    	
				    	var tableHtml = '<tbody>';
				    	var barHtml = '';
				    	$(bugCount).each(function(index,count){
				    		tableHtml += '<tr><td>'+count.name+'</td>';
				    		
				    		var total = 0;
				    		$(bugStatus).each(function(index,status){
				    			var statusId = status.id;
				    			var num = count[statusId];
				    			if (num == null) {
				    				num = 0;
				    			}
				    			total += parseInt(num);
				    			tableHtml += '<td>'+num+'</td>';
				    		});
				    		tableHtml +='<td>'+total+'</td>';
				    		tableHtml += '</tr>'
				    			
				    			/*<div class="col-sm-3">
							<div class="row">
								<div class="checkbox display-inline col-sm-4">
		                            <input id="form-checkbox-1" class="magic-checkbox" type="checkbox" >
		                            <label for="form-checkbox-1">模块1</label>
		                        </div>
								<div  class="col-sm-8 overview-bar" style="margin:15px 0 0;">
									<div style="width: 10%; background-color: rgb(255, 136, 51);"></div>
									<div style="width: 30%; background-color: rgb(255, 206, 84);"></div>
									<div style="width: 45%; background-color: rgb(61, 204, 166);"></div>
									<div style="width: 15%; background-color: rgb(151, 151, 151);"></div>
									</div>
							</div>
						</div>*/
				    		//柱状统计
					    	barHtml += '<div class="col-sm-3">'
					    		+'<div class="row">'
					    		+'<div class="checkbox display-inline col-sm-4">'
					    		//+' <input id="form-checkbox-1" class="magic-checkbox" type="checkbox" >'
					    		+'<label for="form-checkbox-1">'+count.name+'</label>'
					    		+'</div>'
					    		+'<div  class="col-sm-8 overview-bar" style="margin:15px 0 0;">';
					    	
			    			$(bugStatus).each(function(index,status){
				    			var statusId = status.id;
				    			var num = count[statusId];
				    			if (num == null) {
				    				num = 0;
				    			}
				    			//'<div style="width: '+(userData.failure/userData.total)*100+'%; background-color: #979797;"></div>';
				    			if (num != 0) {
				    				barHtml += '<div style="width: '+(num/total)*100+'%; background-color: '+color[index]+'"></div>';
				    			}
				    		});	
			    			barHtml += '</div></div></div>';
				    	});
				    	
				    	$('.bugList').append(barHtml);
				    	
				    	tableHtml += '</tbody>';
				    	$('#bugCountTable').append(tableHtml);
			    	}
			    },
			    complete: function(XMLHttpRequest, textStatus){
		    	}
			});
		},
		
		
		getRandomColor : function(){    
			  return  '#' +    
			    (function(color){    
			    return (color +=  '0123456789abcdef'[Math.floor(Math.random()*16)])    
			      && (color.length == 6) ?  color : arguments.callee(color);    
			  })('');    
			} 
		
};